// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   UploadHelper.java

package com.iphaeton.common.helper;

import com.iphaeton.exception.BusinessException;
import java.io.*;
import org.apache.log4j.Logger;
import org.apache.struts.upload.FormFile;

// Referenced classes of package com.iphaeton.common.helper:
//            IUploadHelper

public class UploadHelper
    implements IUploadHelper
{

    public UploadHelper()
    {
    }

    public void upload(FormFile file, String uploadDir)
        throws BusinessException
    {
        File fileUploadDir = new File(uploadDir);
        try
        {
            if(!fileUploadDir.isDirectory())
                fileUploadDir.mkdirs();
            InputStream stream = file.getInputStream();
            String uploadUrl = uploadDir + file.getFileName();
            logger.info("\u6587\u4EF6\u4E0A\u4F20\u8DEF\u5F84\u4E3A:" + uploadUrl);
            OutputStream bos = new FileOutputStream(uploadUrl);
            int bytesRead = 0;
            byte buffer[] = new byte[8192];
            while((bytesRead = stream.read(buffer, 0, 8192)) != -1) 
                bos.write(buffer, 0, bytesRead);
            bos.close();
            stream.close();
        }
        catch(FileNotFoundException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u627E\u4E0D\u5230\u6307\u5B9A\u6587\u4EF6:" + file.getFileName());
        }
        catch(IOException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u8F93\u5165\u8F93\u51FA\u9519\u8BEF:" + file.getFileName());
        }
    }

    public boolean remove(String name)
        throws BusinessException
    {
        boolean flag = false;
        File file = new File(name);
        if(file.exists())
        {
            flag = file.delete();
            logger.info("\u5220\u9664\u6587\u4EF6:" + name);
        } else
        {
            throw new BusinessException("\u672A\u627E\u5230\u6307\u5B9A\u6587\u4EF6!");
        }
        return flag;
    }

    private static Logger logger;
    static Class class$0; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.common.helper.UploadHelper.class.getName());
    }
}
